package com.digua.baseiot.view

import android.content.Context
import android.util.AttributeSet
import android.widget.RelativeLayout
import com.blankj.utilcode.util.ActivityUtils
import com.digua.baseiot.R
import com.digua.baseiot.databinding.ViewBarBinding

class BarView : RelativeLayout {
    lateinit var ui: ViewBarBinding

    constructor(context: Context?) : super(context) {
        initUI(null)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initUI(attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        initUI(attrs)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        initUI(attrs)
    }

    private fun initUI(attrs: AttributeSet?) {
        ui = ViewBarBinding.bind(inflate(context, R.layout.view_bar, this))
        ui.back.setOnClickListener { ActivityUtils.getTopActivity().finish() }
//        ui.setting.setOnClickListener { context.startActivity(Intent(context, SettingActivity::class.java)) }

        attrs?.let {
            val attr = context.obtainStyledAttributes(it, R.styleable.BarView, 0, 0)
            setTitle(attr.getString(R.styleable.BarView_title))
            val isRightImgShow = attr.getBoolean(R.styleable.BarView_rightImgShow, true)
//            ui.setting.visibility = if (isRightImgShow) VISIBLE else GONE

            val isLeftImgShow = attr.getBoolean(R.styleable.BarView_leftImgShow, true)
            ui.back.visibility = if (isLeftImgShow) VISIBLE else GONE
            attr.recycle()
        }
    }

    fun setTitle(title: String?) {
        title?.let { ui.title.text = it }
    }
}